<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['pluginset']['kf_logo']=tomedia($return['pluginset']['kf_logo']);
$return['pluginset']['topimg']=tomedia($return['pluginset']['topimg']);
$return['pluginset']['bottomimg']=tomedia($return['pluginset']['bottomimg']);
if($_RQ['op']=='main'){
	$merchid=$_RQ['id']?:$_RQ['merchid'];
	if($merchid){
		$myshop=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		$myshop['topimg']=tomedia($myshop['topimg']);
	}
	$return['gametitle']=$myshop['merchname'];
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
	$return['mymid']=$_SESSION['uid'];
	$return['notices']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','id desc');
	$member=model('core/member')->basic($_SESSION['uid']);
	$member['createtime']=date('Y-m-d',$return['member']['createtime']);
	$member['ordernum']=pdo_count('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$member['mylike']=pdo_count('jy_merch_game_item_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$return['member']=$member;
	$return['params']=array('gamename'=>'jy_merchgame_ms','merchid'=>$merchid);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0,'merchid'=>$merchid,'video <>'=>'');
	$params['gamename']='jy_merchgame_fx';
	$allitemgame=$this->allitemgame;
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 5;
	$list=pdo_getslice('jy_merch_game_item',$params,array($pindex,$psize),$total,$fields,'','totop desc, paixu desc, createtime desc');
	if($list){
		$sum=count($list);
		foreach($list as $lk=>&$item){
			$item['gameset']=json_decode($item['gameset'],true);
			$item['logo']=tomedia($item['logo']);
			$item['member']=pdo_get('jy_merch_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('merchname','logo','id'));
			$item['member']['logo']=tomedia($item['member']['logo']);
			if(!$item['price'] || $item['price']<0){
				$item['goods']=json_decode($item['goods'],true);
				$item['price']=$item['goods'][0]['price'];
			}
			$item['price']=price($item['price']);
			$item['typename']=$allitemgame[$item['gamename']]['title'];
			$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']);
			$members=pdo_getslice('jy_merch_game_order',$params,array($pindex,$psize),$total,array('mid','createtime'),'','createtime desc');
			if($members){
				foreach($members as &$m){
					$m['createtime'] = date('Y-m-d H:i',$m['createtime']);
					$m['member'] = model('core/member')->basic($m['mid']);
				}
			}
			$item['jielinum']=pdo_count('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'id');
			$item['members']=$members;
			if($item['onlinetime']<time()){
				$item['status']=2;
				$item['statustitle']='已结束';
			}else{
				$item['status']=1;
				$item['statustitle']='进行中';
			}
			$item['onlinetime'] = date('Y-m-d H:i',$item['onlinetime']);
			if(strpos($item['gamename'],'tool_') !== false){
				$item['isdiyform']=1;
			}elseif(strpos($item['gamename'],'game_') !== false){
				$item['isdiygoods']=1;
			}
			$gv=$item['goods'][0];
			$item['lastnum']=$gv['total'];
			$item['isbuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>0),'total');
			$item['mybuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>0),'total');
			if($item['isbuynum']){
				if(!$gv['total']){
					$item['percent']=-1;
				}else{
					$total=$gv['total']?:100;
					$item['lastnum']=$total-$item['isbuynum'];
					$item['percent']=intval(100*$item['isbuynum']/$total);
				}
			}
			$item['fxfee01']=$gv['fxfee01'];
			$isup=pdo_get('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'orderid >'=>0,'pid'=>$item['id'],'mid'=>$_SESSION['uid'],'fromuser'=>-1));
			if($isup['id']){
				$item['teamnum']=$isup['teamnum']?:2;
				$item['lastnum']=$item['teamnum']-$isup['isteamnum'];
				$item['percent']=$item['lastnum']/$item['teamnum'];
			}else{
				$item['lastnum']=$item['gameset']['teamnum'];
			}
		}
	}
	$return['fxlist']=$list;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0);
	$params['gamename']='jy_merchgame_pt';
	$allitemgame=$this->allitemgame;
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 5;
	$list=pdo_getslice('jy_merch_game_item',$params,array($pindex,$psize),$total,$fields,'','totop desc, paixu desc, createtime desc');
	if($list){
		$sum=count($list);
		foreach($list as $lk=>&$item){
			$item['gameset']=json_decode($item['gameset'],true);
			$item['logo']=tomedia($item['logo']);
			$item['member']=pdo_get('jy_merch_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('merchname','logo','id'));
			$item['member']['logo']=tomedia($item['member']['logo']);
			if(!$item['price'] || $item['price']<0){
				$item['goods']=json_decode($item['goods'],true);
				$item['price']=$item['goods'][0]['price'];
			}
			$item['price']=price($item['price']);
			$item['typename']=$allitemgame[$item['gamename']]['title'];
			$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']);
			$members=pdo_getslice('jy_merch_game_order',$params,array($pindex,$psize),$total,array('mid','createtime'),'','createtime desc');
			if($members){
				foreach($members as &$m){
					$m['createtime'] = date('Y-m-d H:i',$m['createtime']);
					$m['member'] = model('core/member')->basic($m['mid']);
				}
			}
			$item['jielinum']=pdo_count('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'id');
			$item['members']=$members;
			if($item['onlinetime']<time()){
				$item['status']=2;
				$item['statustitle']='已结束';
			}else{
				$item['status']=1;
				$item['statustitle']='进行中';
			}
			$item['onlinetime'] = date('Y-m-d H:i',$item['onlinetime']);
			if(strpos($item['gamename'],'tool_') !== false){
				$item['isdiyform']=1;
			}elseif(strpos($item['gamename'],'game_') !== false){
				$item['isdiygoods']=1;
			}
			$gv=$item['goods'][0];
			$item['lastnum']=$gv['total'];
			$item['isbuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>0),'total');
			$item['mybuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>0),'total');
			if($item['isbuynum']){
				if(!$gv['total']){
					$item['percent']=-1;
				}else{
					$total=$gv['total']?:100;
					$item['lastnum']=$total-$item['isbuynum'];
					$item['percent']=intval(100*$item['isbuynum']/$total);
				}
			}
			$item['fxfee01']=$gv['fxfee01'];
			$item['canyunum']=pdo_count('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'fromuser'=>-1));
		}
	}
	$return['ptlist']=$list;
	$return['myshop']=$myshop;
	set_html('title',$return['gametitle']?:'活动助手');
	$share_image=$return['pluginset']['share_desc']?:'../addons/jy_quanhuo/resource/images/logo.png';
	$return['sharedata']=array(
			'title'=>$return['pluginset']['share_title']?:'活动助手',
			'desc'=>$return['pluginset']['share_desc']?:'商家节假日 老带新 裂变引流 成交复购必备工具',
			'link'=>custom_url('jy_videomerch/myshop//id='.$merchid.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$share_image,
	);
	$return['params']['page'] =1;
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0);
	if($_RQ['gamename']){
		$params['gamename']=$_RQ['gamename'];
	}
	if($_RQ['merchid']){
		$params['merchid']=$_RQ['merchid'];
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.$_RQ['keyword'].'%';
			$return['params']['title']=$_RQ['keyword'];
		}
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
	}
	$allitemgame=$this->allitemgame;
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_merch_game_item',$params,array($pindex,$psize),$total,$fields,'','totop desc, paixu desc, createtime desc');
	if($list){
		$sum=count($list);
		foreach($list as $lk=>&$item){
			$item['gameset']=json_decode($item['gameset'],true);
			$item['logo']=tomedia($item['logo']);
			$item['member']=pdo_get('jy_merch_user',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('merchname','logo','id'));
			$item['member']['logo']=tomedia($item['member']['logo']);
			if(!$item['price'] || $item['price']<0){
				$item['goods']=json_decode($item['goods'],true);
				$item['price']=$item['goods'][0]['price'];
			}
			$item['price']=price($item['price']);
			$item['typename']=$allitemgame[$item['gamename']]['title'];
			$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']);
			$members=pdo_getslice('jy_merch_game_order',$params,array($pindex,$psize),$total,array('mid','createtime'),'','createtime desc');
			if($members){
				foreach($members as &$m){
					$m['createtime'] = date('Y-m-d H:i',$m['createtime']);
					$m['member'] = model('core/member')->basic($m['mid']);
				}
			}
			$item['jielinum']=pdo_count('jy_merch_game_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']));
			$item['members']=$members;
			if($item['onlinetime']<time()){
				$item['status']=2;
				$item['statustitle']='已结束';
			}else{
				$item['status']=1;
				$item['statustitle']='进行中';
			}
			$item['onlinetime'] = date('Y-m-d H:i',$item['onlinetime']);
			if(strpos($item['gamename'],'tool_') !== false){
				$item['isdiyform']=1;
			}elseif(strpos($item['gamename'],'game_') !== false){
				$item['isdiygoods']=1;
			}
			$gv=$item['goods'][0];
			$item['lastnum']=$gv['total'];
			$item['isbuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>0),'total');
			$item['mybuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>0),'total');
			if($item['isbuynum']){
				if(!$gv['total']){
					$item['percent']=-1;
				}else{
					$total=$gv['total']?:100;
					$item['lastnum']=$total-$item['isbuynum'];
					$item['percent']=intval(100*$item['isbuynum']/$total);
				}
			}
			$item['fxfee01']=$gv['fxfee01'];
			$item['canyunum']=pdo_count('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'fromuser'=>-1));
			$isup=pdo_get('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'orderid >'=>0,'pid'=>$item['id'],'mid'=>$_SESSION['uid'],'fromuser'=>-1));
			if($isup['id']){
				$item['teamnum']=$isup['teamnum']?:2;
				$item['lastnum']=$item['teamnum']-$isup['isteamnum'];
				$item['percent']=$item['lastnum']/$item['teamnum'];
			}else{
				$item['lastnum']=$item['gameset']['teamnum'];
			}
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='setmsg'){
	$postdata=get_postdata();
	if(!$postdata['msg']){
		exi('请输入评论内容！','error');
	}
	$snsid = $this->session('snsid');
	$item=pdo_get('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($postdata['pid'])));
	$checked = ($basic['msg_shenhe'] || $basic['open_wxappsh'])?0:1;
	$data=array(
		'title'=>trim($postdata['msg']),
		'images'=>ijson_encode($postdata['images']),
		'mid'=>$_SESSION['uid'],
		'pid'=>intval($postdata['pid']),
		'uniacid'=>$_SESSION['uniacid'],
		'createtime'=>time(),
		'checked'=>$checked,
		'type'=>$item['type'],
	);
	pdo_insert('jy_merch_game_item_msg',$data);
	if($checked){
		exi('评论成功！');
	}else{
		exi('评论成功！等待审核！');
	}
}
if($_RQ['op']=='shoucang'){
	$postdata=get_postdata();
	$id = intval($postdata['pid']);
	if(!$id){
		exi('没有找到该帖子，请重新选择！','error');
	}
	$shoucang=pdo_get('jy_merch_game_item_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	if($shoucang){
		if($shoucang['islike'] !=1){
			pdo_update('jy_merch_game_item_like',array('islike'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			exi('关注成功');
		}else{
			pdo_update('jy_merch_game_item_like',array('islike'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
		   exi('取消关注');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'pid'=>$id,
			'mid'=>$_SESSION['uid'],
			'islike'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_merch_game_item_like',$insert);
	}
	exi('关注成功');
}
if($_RQ['op']=='upfangan'){
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	if(!$id){
		exi('没有找到方案，请重新选择！','error');
	}
	$fangan=pdo_get('jy_merch_game_fangan',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	pdo_update('jy_merch_game_fangan',array('isnum +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('领取成功');
}
if($_RQ['op']=='getnotice'){
	$log=cfc('message')->getonelog();
	exi($log['msg']);
}